//
//  ANTabBarController.m
//  ShenWeiRent
//
//  Created by 李叶夫 on 2019/2/27.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "ANTabBarController.h"
#import "ANNavigationController.h"
#import "ANMainController.h"
#import "ANAroundController.h"
#import "ANMineController.h"

@interface ANTabBarController ()<UITabBarControllerDelegate>

@end

@implementation ANTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self addChildVcs];
    
    self.delegate = self;
}

- (void)addChildVcs
{
    ANMainController *mainVc = [[ANMainController alloc] init];
    mainVc.title = @"首页";
    ANAroundController *aroundVc = [[ANAroundController alloc] init];
    aroundVc.title = @"周边店面";
    ANMineController *mineVc = [[ANMineController alloc] init];
    
    [self addChildController:mainVc imageName:@"a_home2" selectedImageName:@"a_home1" title:@"首页"];
    [self addChildController:aroundVc imageName:@"a_store2" selectedImageName:@"a_store1" title:@"周边店面"];
    [self addChildController:mineVc imageName:@"a_person2" selectedImageName:@"a_person1" title:@"个人中心"];
}

- (void)addChildController:(ANBaseViewController *)childVc imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName title:(NSString *)title
{
    childVc.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.title = title;
    
    ANNavigationController *navi = [[ANNavigationController alloc] initWithRootViewController:childVc];
    [self addChildViewController:navi];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    ANNavigationController *navi = (ANNavigationController *)viewController;
    UIViewController *rootVc = navi.viewControllers.firstObject;
    if ([rootVc isKindOfClass:[ANMineController class]]) {
        if (UserUtils.shared.isLogin) {
            return YES;
        }
        else {
            [UserUtils showLoginVc];
            return NO;
        }
    }
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
